package logic

import (
	"context"
	"renting-demo/common"
	"renting-demo/user_srv/user"

	"renting-demo/api/internal/svc"
	"renting-demo/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type SendLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSendLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendLogic {
	return &SendLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SendLogic) Send(req *types.SendRequest) (resp *types.Data, err error) {
	// todo: add your logic here and delete this line
	if !common.Verify(req.Mobile, "mobile") {
		return &types.Data{
			Code:    500,
			Message: "手机号格式不正确",
		}, nil
	}
	res, err := l.svcCtx.UserServer.UserSendMobile(context.Background(), &user.UserSendMobileRequest{
		Mobile: req.Mobile,
	})
	if err != nil {
		return &types.Data{
			Code:    500,
			Message: err.Error(),
		}, nil
	}
	return &types.Data{
		Code:    0,
		Message: "发送成功,五分钟内有效",
		Data:    map[string]string{"SMS": res.Code},
	}, nil
}
